package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.dao.CourseDao;
import com.example.demo.entity.Course;
import com.example.demo.service.CourseService;
import org.jsoup.Connection;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;



/**
 * (Course)表服务实现类
 *
 * @author makejava
 * @since 2021-03-05 09:48:21
 */
@Service("courseService")
public class CourseServiceImpl extends ServiceImpl<CourseDao, Course> implements CourseService {

    @Resource
    CourseService courseService;

    @Override
    public Map<String,String> getCourses(String name) throws Exception {
        Course course = courseService.getByName(name);
        Map<String,String> courses = new HashMap<>();
        // 获取对象属性
        Field[] fields = course.getClass().getDeclaredFields();
        for(Field field : fields){
            field.setAccessible(true);
            courses.put(field.getName(),field.get(course).toString());
        }
        return courses;
    }

    @Override
    public Course getByName(String name) {
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",name);
        return courseService.getOne(queryWrapper);
    }

    @Override
    public Course getCourse(Connection connection) throws IOException, IllegalAccessException {
        Course course = new Course();
        course.setXqh_id(connection.get().getElementById("xqh_id").val());
        course.setJg_id(connection.get().getElementById("jg_id_1").val());
        course.setZyh_id(connection.get().getElementById("zyh_id").val());
        course.setZyfx_id(connection.get().getElementById("zyfx_id").val());
        course.setNjdm_id(connection.get().getElementById("njdm_id").val());
        course.setBh_id(connection.get().getElementById("bh_id").val());
        course.setXbm(connection.get().getElementById("xbm").val());
        course.setXslbdm(connection.get().getElementById("xslbdm").val());
        course.setCcdm(connection.get().getElementById("ccdm").val());
        course.setXsbj(connection.get().getElementById("xsbj").val());
        course.setXkxnm(connection.get().getElementById("xkxnm").val());
        course.setXkxqm(connection.get().getElementById("xkxqm").val());


        return course;
    }

}